
library("ggplot2")

library("datasets")#加载库

data()  # 显示所有数据集

?mtcars  # 显示数据集mtcars的帮助文档

data(mtcars)
dim(mtcars)  # 显示数据集的行数和列数
colnames(mtcars)  # 显示列名
rownames(mtcars)  # 显示行名

mpg_mean <- round(mean(mtcars$mpg), 1)  # 计算油耗的平均值
disp_max <- round(max(mtcars$disp), 1)  # 计算排量的最大值
disp_min <- round(min(mtcars$disp), 1)  # 计算排量的最小值

#随机选车函数
getrandomcar <- function(data) {
  sample(data$cyl, 1)
}

set.seed(123)  # 设置随机数种子以获得可重复的结果
cyl_counts <- replicate(100, getrandomcar(mtcars))

qplot(cyl_counts,binwidth = 1);#随机抽取重复100次，通过直方图展示结果
plot(mtcars$hp, mtcars$cyl, main = "HP - Cylinders", xlab = "马力", ylab = "发动机气缸数")#“总马力”和“发动机气缸数目”之间的散点图


# hist(cyl_counts, main = "Cylinder Counts", xlab = "Cylinders")
